package cn.exrick.xboot.modules.iocr.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.iocr.entity.ZJGLServiceIocr;
import cn.exrick.xboot.modules.iocr.service.ZJGLServiceIocrService;
import cn.exrick.xboot.modules.iocr.util.IocrUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 王延超
 */
@Slf4j
@RestController
@Api(tags = "识别入库实体类管理接口")
@RequestMapping("/xboot/ZJGLServiceIocr")
@Transactional
public class ZJGLServiceIocrController {

    @Autowired
    private ZJGLServiceIocrService ZJGLServiceIocrService;

    @PostMapping("/test")
    public Result test(@RequestBody Map fileData) {
        int i = ZJGLServiceIocrService.iocrServe(fileData);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<ZJGLServiceIocr> get(@PathVariable Long id) {

        ZJGLServiceIocr ZJGLServiceIocr = ZJGLServiceIocrService.getById(id);
        return new ResultUtil<ZJGLServiceIocr>().setData(ZJGLServiceIocr);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<ZJGLServiceIocr>> getAll() {

        List<ZJGLServiceIocr> list = ZJGLServiceIocrService.list();
        return new ResultUtil<List<ZJGLServiceIocr>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<ZJGLServiceIocr>> getByPage(PageVo page) {

        IPage<ZJGLServiceIocr> data = ZJGLServiceIocrService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<ZJGLServiceIocr>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<ZJGLServiceIocr> saveOrUpdate(ZJGLServiceIocr ZJGLServiceIocr) {
        if (ZJGLServiceIocr.getId() != null)
            ZJGLServiceIocr.setState("2");
        if (ZJGLServiceIocrService.saveOrUpdate(ZJGLServiceIocr)) {
            return new ResultUtil<ZJGLServiceIocr>().setData(ZJGLServiceIocr);
        }
        return new ResultUtil<ZJGLServiceIocr>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<ZJGLServiceIocr>> getByCondition(ZJGLServiceIocr zJGLServiceIocr,
                                                         PageVo page, SearchVo searchVo) throws Exception {

        QueryWrapper<ZJGLServiceIocr> qw = new QueryWrapper<>();
        // 收款类型模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getType()), "type", zJGLServiceIocr.getType());
        // 地市模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getCity()), "city", zJGLServiceIocr.getCity());
        // 收款时间相等匹配
        if (searchVo.getStartDate() != "" && searchVo.getEndDate() != "") {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(searchVo.getEndDate()));
            cal.add(Calendar.DAY_OF_MONTH, 1);//加1天
            searchVo.setEndDate(sdf.format(cal.getTime()));
            Date start = sdf.parse(searchVo.getStartDate());
            Date end = sdf.parse(searchVo.getEndDate());
            qw.between("collection_time", start, end);
        }
//        qw.eq(zJGLServiceIocr.getCollectionTime() != null, "collection_time", zJGLServiceIocr.getCollectionTime());
        // 金额模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getSum()), "sum", zJGLServiceIocr.getSum());
        // 付款方模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getPayer()), "payer", zJGLServiceIocr.getPayer());
        // 备注模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getRemark()), "remark", zJGLServiceIocr.getRemark());
        // 汇款单号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getBillNumber()), "bill_number", zJGLServiceIocr.getBillNumber());
        // 收款卡号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getCreditCardNumbers()), "credit_card_numbers", zJGLServiceIocr.getCreditCardNumbers());
        // 付款卡号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getPaymentCardNumber()), "payment_card_number", zJGLServiceIocr.getPaymentCardNumber());
        // 项目编号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getProjectNumber()), "project_number", zJGLServiceIocr.getProjectNumber());
        // BSP项目编号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getBspProjectNumber()), "bsp_project_number", zJGLServiceIocr.getBspProjectNumber());
        // 状态:1: 未认领,2: 已认领模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getState()), "state", zJGLServiceIocr.getState());
        // 关联图片id模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getImgFileId()), "img_file_id", zJGLServiceIocr.getImgFileId());
        // 流水号模糊搜素
        qw.like(StrUtil.isNotBlank(zJGLServiceIocr.getSerialNumber()), "serial_number", zJGLServiceIocr.getSerialNumber());
        qw.orderBy(true,true,"state");
        IPage<ZJGLServiceIocr> data = ZJGLServiceIocrService.page(PageUtil.initMpPage(page), qw);
        return new ResultUtil<IPage<ZJGLServiceIocr>>().setData(data);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result delAllByIds(@RequestParam Long[] ids) {

        for (Long id : ids) {
            ZJGLServiceIocrService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
